package com.zcf.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alipay.api.internal.util.StringUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zcf.common.json.Body;
import com.zcf.pojo.Dining;
import com.zcf.pojo.Product;
import com.zcf.pojo.Shopp;
import com.zcf.pojo.Shopping;
import com.zcf.pojo.User;
import com.zcf.service.ShoppService;
import com.zcf.utils.Hutool;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 购物表 前端控制器
 * </p>
 *
 * @author loupeng
 * @since 2018-12-20
 */
@Controller
@RequestMapping("/shopp")
@ResponseBody
@CrossOrigin
public class ShoppController {

	@Autowired
	ShoppService service;

	/**
	 * 查询当前商店购物车
	 */
	@PostMapping("queryShopping")
	public Body queryShopping(Shopp shopp) {
		return service.queryShopping(shopp);
	}

	/**
	 * 查询购物车商品详情
	 */
	@PostMapping("addShoppinges")
	public Body addShoppinges(Shopping shopp) {
		Shopping shopping = new Shopping();
		EntityWrapper<Shopp> ew = new EntityWrapper<>();
		ew.eq("user_id", shopp.getUserId());
		if (shopp.getShoppingId() != null) {
			ew.eq("shopping_id", shopp.getShoppingId());
		}
		if (shopp.getDinId() != null) {
			ew.eq("din_id", shopp.getDinId());
		}
		shopping = shopping.selectOne(ew);
		if (shopping == null) {
			return Body.newInstance("购物车不存在");
		}

		Dining dining = new Dining();
		EntityWrapper<Dining> din = new EntityWrapper<>();
		din.eq("id", shopp.getDinId());
		List<Dining> liding = dining.selectList(din);
		if (liding.size() > 0) {
			shopping.setDining(liding);
		}

		Shopp shopp2 = new Shopp();
		EntityWrapper<Shopp> ewq = new EntityWrapper<>();
		ewq.eq("shopping_id", shopping.getShoppingId());
		List<Shopp> listShopp = shopp2.selectList(ewq);
		if (listShopp.size() > 0) {
			shopping.setShopp(listShopp);
			shopping.setDining(liding);
			double sum = 0;
			int number = 0;
			for (Shopp shopp3 : listShopp) {
				Product product = new Product();
				EntityWrapper<Product> ewr = new EntityWrapper<>();
				ewr.eq("id", shopp3.getProductId());
				product = product.selectOne(ewr);
				shopp3.setProduct_name(product.getProductName());
				if (product.getImage() != null) {
					shopp3.setImage(product.getImage());
				}
				if (shopp3.getNumber().equals("0")) {
					Shopp shopps = new Shopp();
					shopps.setShoppId(shopp3.getShoppId());
					shopps.deleteById();
				}
				if (shopp3.getTotalPrice() != null) {
					sum += shopp3.getTotalPrice();
				}
				if (!StringUtils.isEmpty(shopp3.getNumber())) {
					number += Integer.parseInt(shopp3.getNumber());
				}
			}
			int money = 0;
			for (Dining dining1 : liding) {
				money += dining1.getBox() + dining1.getFreight();
			}
			shopping.setTotal(sum + money + "");
			shopping.setNumber(number);
			Boolean flag = shopping.updateById();
			if (flag) {
				if (shopping.getNumber() == 0) {
					shopping.deleteById();
				}
			}
		}
		return Body.newInstance(shopping);
	}

	/**
	 * 添加购物车或修改购物车
	 */
	@PostMapping("insertShopp")
	public Body insertShoppXin(Shopp shopp, Integer num) {
		Shopping shopping = new Shopping();
		EntityWrapper<Shopping> ew = new EntityWrapper<>();
		ew.eq("din_id", shopp.getDinId());
		ew.eq("user_id", shopp.getUserId());
		shopping = shopping.selectOne(ew);
		Shopping shopping2 = new Shopping();
		if (shopping == null) {
			shopping2.setShoppingId(Hutool.getId());
			shopping2.setAddtime(Hutool.parseDateToString());
			shopping2.setDinId(shopp.getDinId());
			shopping2.setUserId(shopp.getUserId());
			shopping2.setNumber(1);
			shopping2.setTotal(shopp.getTotalPrice() + "");
			Boolean flag = shopping2.insert();
			if (flag) {
				shopp.setShoppId(Hutool.createId());
				shopp.setShoppingId(shopping2.getShoppingId());
				shopp.setAddtime(Hutool.parseDateToString());
				if (num == 1) {
					if (StringUtils.isEmpty((shopp.getNumber()))) {
						shopp.setNumber("0");
						shopp.setNumber(Integer.parseInt(shopp.getNumber()) + 1 + "");
						Double s = shopp.getPrice() * Integer.parseInt(shopp.getNumber());
						shopp.setTotalPrice(s);
					} else {
						shopp.setNumber("1");
						Double s = shopp.getPrice() * Integer.parseInt(shopp.getNumber());
						shopp.setTotalPrice(s);
					}
				} else {
					if (!StringUtils.isEmpty((shopp.getNumber()))) {
						shopp.setNumber(Integer.parseInt(shopp.getNumber()) - 1 + "");
						Double s = shopp.getPrice() * Integer.parseInt(shopp.getNumber());
						shopp.setTotalPrice(s);
					} else {
						shopp.setNumber("0");
						shopp.setTotalPrice(0.0);
					}
				}
				Boolean falg = false;
				if (num == 1) {
					falg = shopp.insert();
				}
				if (falg) {
					Shopping shopping3 = new Shopping();
					shopping3.setShoppingId(shopping2.getShoppingId());
					shopping3.setUserId(shopp.getUserId());
					shopping3.setDinId(shopp.getDinId());
					return Body.newInstance(this.addShoppinges(shopping3));
				}
				return Body.newInstance("添加失败");
			}
		} else {
			Shopp shopp2 = new Shopp();
			EntityWrapper<Shopp> ews = new EntityWrapper<>();
			ews.eq("shopping_id", shopping.getShoppingId());
			ews.eq("product_id", shopp.getProductId());
			shopp2 = shopp2.selectOne(ews);
			if (shopp2 != null) {
				shopp2.setDinId(shopp.getDinId());
				shopp2.setProductId(shopp.getProductId());
				if (num == 1) {
					if (!StringUtils.isEmpty((shopp.getNumber()))) {
						shopp2.setNumber("0");
						shopp2.setNumber(Integer.parseInt(shopp2.getNumber()) + 1 + "");
						Double s = shopp2.getPrice() * Integer.parseInt(shopp2.getNumber());
						shopp.setTotalPrice(s);
					} else {
						shopp2.setNumber(Integer.parseInt(shopp2.getNumber()) + 1 + "");
						Double s = shopp2.getPrice() * Integer.parseInt(shopp2.getNumber());
						shopp.setTotalPrice(s);
					}
				} else {
					if (!StringUtils.isEmpty((shopp2.getNumber()))) {
						shopp2.setNumber(Integer.parseInt(shopp2.getNumber()) - 1 + "");
						Double s = shopp2.getPrice() * Integer.parseInt(shopp2.getNumber());
						shopp.setTotalPrice(s);
					} else {

						shopp.deleteById();
					}
				}
				shopp2.setPrice(shopp.getPrice());
				shopp2.setTotalPrice(shopp.getTotalPrice());
				shopp2.setAddtime(Hutool.parseDateToString());
				shopp2.setUserId(shopp.getUserId());
				shopp2.setShoppId(shopp2.getShoppId());
				shopp2.setShoppingId(shopp2.getShoppingId());
				shopp2.setFlavor(shopp.getFlavor());
				Boolean flags = shopp2.updateById();
				if (flags) {
					Shopping shopping3 = new Shopping();
					shopping3.setShoppingId(shopping.getShoppingId());
					shopping3.setUserId(shopp.getUserId());
					shopping3.setDinId(shopp.getDinId());
					return Body.newInstance(this.addShoppinges(shopping3));
				}
				return Body.newInstance("修改购物车失败");
			} else if (shopp2 == null) {
				shopp.setShoppId(Hutool.createId());
				shopp.setShoppingId(shopping.getShoppingId());
				shopp.setAddtime(Hutool.parseDateToString());
				if (num == 1) {
					if (StringUtils.isEmpty((shopp.getNumber()))) {
						shopp.setNumber("0");
						shopp.setNumber(Integer.parseInt(shopp.getNumber()) + 1 + "");
						Double s = shopp.getPrice() * Integer.parseInt(shopp.getNumber());
						shopp.setTotalPrice(s);
					} else {
						shopp.setNumber(Integer.parseInt(shopp.getNumber()) + 1 + "");
						Double s = shopp.getPrice() * Integer.parseInt(shopp.getNumber());
						shopp.setTotalPrice(s);
					}
				} else {
					if (!StringUtils.isEmpty((shopp.getNumber()))) {
						shopp.setNumber(Integer.parseInt(shopp.getNumber()) - 1 + "");
						Double s = shopp.getPrice() * Integer.parseInt(shopp.getNumber());
						shopp.setTotalPrice(s);
					} else {
						shopp.setNumber("0");
						shopp.setTotalPrice(0.0);
						shopp.deleteById();
					}
				}
				Boolean falg = true;
				if (num == 1) {
					falg = shopp.insert();
				}
				if (falg) {
					Shopping shopping3 = new Shopping();
					shopping3.setShoppingId(shopping.getShoppingId());
					shopping3.setUserId(shopp.getUserId());
					shopping3.setDinId(shopp.getDinId());
					return Body.newInstance(this.addShoppinges(shopping3));
				}
				return Body.newInstance("添加失败");
			}
		}
		return Body.BODY_500;
	}

	/**
	 * 添加购物车 废
	 * 
	 * @PostMapping("insertShopplps") public Body insertShopp(Shopp shopp) { User
	 * user = new User(); user.setId(shopp.getUserId()); user = user.selectById();
	 * if (user == null) { return Body.newInstance("用户不存在！！！"); }
	 * 
	 * Shopping shopping = new Shopping(); shopping.setUserId(shopp.getUserId());
	 * shopping = shopping.selectById(); // 创建购物车 Shopping shopping2 = new
	 * Shopping(); if (shopping != null) { shopping2.setShoppingId(Hutool.getId());
	 * shopping2.setAddtime(Hutool.parseDateToString());
	 * shopping2.setDinId(shopp.getDinId()); shopping2.setUserId(shopp.getUserId());
	 * shopping2.setNumber(shopping.getNumber() + 1);
	 * shopping2.setTotal(shopp.getTotalPrice() + ""); shopping2.insert(); } else if
	 * (shopping == null) { shopping2.setShoppingId(Hutool.getId());
	 * shopping2.setAddtime(Hutool.parseDateToString());
	 * shopping2.setDinId(shopp.getDinId()); shopping2.setUserId(shopp.getUserId());
	 * shopping2.setNumber(1); shopping2.setTotal(shopp.getTotalPrice() + "");
	 * shopping2.insert(); }
	 * 
	 * EntityWrapper<Shopp> ew = new EntityWrapper<>(); ew.eq("user_id",
	 * shopp.getUserId()); ew.eq("din_id", shopp.getDinId()); Shopp shopping1 = new
	 * Shopp(); shopping1 = shopping1.selectOne(ew); if (shopping1 == null) { Shopp
	 * shopp2 = new Shopp(); String[] productId = shopp.getProductId().split(",");
	 * String[] number = shopp.getProductId().split(","); String price = "" +
	 * shopp.getPrice(); String[] prices = price.split(","); String[] flavor =
	 * shopp.getFlavor().split(","); for (int i = 0; i < productId.length; i++) {
	 * double totalPrice = 0; shopp2.setShoppId(Hutool.createId());
	 * shopp2.setShoppingId(shopping2.getShoppingId());
	 * shopp2.setAddtime(Hutool.parseDateToString());
	 * shopp2.setUserId(shopp.getUserId()); shopp2.setDinId(shopp.getDinId());
	 * shopp2.setProductId(productId[i]); shopp2.setNumber(number[i]);
	 * shopp2.setPrice(Double.parseDouble(prices[i])); shopp2.setFlavor(flavor[i]);
	 * totalPrice = Double.parseDouble(number[i]) * Double.parseDouble(prices[i]);
	 * shopp2.setTotalPrice(totalPrice); shopp2.insert(); } return
	 * Body.newInstance(shopp2); } else { Shopp shopp2 = new Shopp(); String[]
	 * productId = shopp.getProductId().split(","); String[] number =
	 * shopp.getProductId().split(","); String price = "" + shopp.getPrice();
	 * String[] prices = price.split(","); String[] flavor =
	 * shopp.getFlavor().split(","); for (int i = 0; i < productId.length; i++) {
	 * double totalPrice = 0; shopp2.setShoppId(Hutool.createId());
	 * shopp2.setShoppingId(shopping2.getShoppingId());
	 * shopp2.setAddtime(Hutool.parseDateToString());
	 * shopp2.setUserId(shopp.getUserId()); shopp2.setDinId(shopp.getDinId());
	 * shopp2.setProductId(productId[i]); shopp2.setNumber(number[i]);
	 * shopp2.setPrice(Double.parseDouble(prices[i])); shopp2.setFlavor(flavor[i]);
	 * totalPrice = Double.parseDouble(number[i]) * Double.parseDouble(prices[i]);
	 * shopp2.setTotalPrice(totalPrice); shopp2.insert(); } return
	 * Body.newInstance(shopp2); } }
	 */

	public static void main(String[] args) {
		System.err.println(Hutool.parseDateToString());
	}
}
